//
//  SecondView.m
//  第九节作业
//
//  Created by dllo on 16/3/21.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "SecondView.h"
#import "Album.h"
#import "MyScroller.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface SecondView ()<UIScrollViewDelegate>
@property(nonatomic, retain)MyScroller *scrolleView;
@property(nonatomic, retain)UIPageControl *page;

@end

@implementation SecondView

- (void)dealloc
{
    [_page release];
    [_scrolleView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createSubView];
    self.navigationItem.title = @"第二页";
    self.view.backgroundColor = [UIColor yellowColor];
}
- (void)createSubView {
    self.scrolleView = [[MyScroller alloc] initWithFrame:CGRectMake(0, 0,WIDTH, 600)];
    self.scrolleView.contentSize = CGSizeMake(WIDTH * 9, 0);
    self.scrolleView.contentOffset = CGPointMake(WIDTH, 0);
    self.scrolleView.pagingEnabled = YES;
    self.scrolleView.showsHorizontalScrollIndicator = NO;
    self.scrolleView.delegate = self;
    [self.view addSubview:self.scrolleView];
    [self.scrolleView release];
    //创建第一个小scorlleView
    MyScroller *smallFirstOfScorlleView = [[MyScroller alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 600)];
    UIImageView *imageViewOf1rd = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 600)];
    smallFirstOfScorlleView.minimumZoomScale = 1.0;
    smallFirstOfScorlleView.maximumZoomScale = 2.5;
    smallFirstOfScorlleView.delegate = self;
    imageViewOf1rd.image = [UIImage imageNamed:@"hzw7.jpg"];
    [smallFirstOfScorlleView addSubview:imageViewOf1rd];
    [self.scrolleView addSubview:smallFirstOfScorlleView];
    [smallFirstOfScorlleView release];
    // 创建中间的7个小scrollView
    for (int i = 1; i < 8; i++) {
        MyScroller *smallScrollView = [[MyScroller alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, 600)];
        smallScrollView.minimumZoomScale = 1.0;
        smallScrollView.maximumZoomScale = 2.5;
        smallScrollView.delegate = self;
        NSString *name = [NSString stringWithFormat:@"hzw%d.jpg",i];
        
        UIImageView *smallView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 600)];
        smallView.image = [UIImage imageNamed:name];
        [smallScrollView addSubview:smallView];
        [self.scrolleView addSubview:smallScrollView];
        [smallScrollView release];
        [smallView release];
    }
    MyScroller *smallLastOfScrolleView = [[MyScroller alloc] initWithFrame:CGRectMake(WIDTH * 8, 0, WIDTH, 600)];
    UIImageView *imageViewOfLast = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 600)];
    smallLastOfScrolleView.minimumZoomScale = 1.0;
    smallLastOfScrolleView.maximumZoomScale = 2.5;
    smallLastOfScrolleView.delegate = self;
    imageViewOfLast.image = [UIImage imageNamed:@"hzw1.jpg"];
    [smallLastOfScrolleView addSubview:imageViewOfLast];
    [self.scrolleView addSubview:smallLastOfScrolleView];
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 400, WIDTH - 200, 20)];
    
    self.page.numberOfPages = 7;
    [self.view addSubview:self.page];
    
    
}







// 图片到头的时候会卡顿一下..
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.page.currentPage = self.scrolleView.contentOffset.x / WIDTH - 1;
    
    if (self.scrolleView.contentOffset.x == 0) {
        self.scrolleView.contentOffset = CGPointMake(WIDTH * 7, 0);
    }
    else if (self.scrolleView.contentOffset.x == WIDTH * 8){
        self.scrolleView.contentOffset = CGPointMake(WIDTH, 0);
        
        
    }
    for (UIScrollView *sScrollView in scrollView.subviews) {
        if ([sScrollView isKindOfClass:[UIScrollView class]]) {
            sScrollView.zoomScale = 1.0;
        }
    }
    
    
    
    
    
    
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    UIView *view111 = [scrollView.subviews objectAtIndex:0];
    return   view111;
    
    
    
    
}


//
//- (NSArray *)handlePage{
//    NSMutableArray *mArr = [NSMutableArray array];
//    
//    for (int i = 1; i < 8; i++) {
//        
//        NSString *name = [NSString stringWithFormat:@"hzw%d.jpg", i];
//        
//        UIImage *image = [UIImage imageNamed:name];
//        
//        [mArr addObject:image];
//    }
//    
//    return mArr;
//    
//}
//
//- (void)createAlbum {
//    
//    Album *album = [[Album alloc] initWithFrame:CGRectMake(0, 0, 300, 400) withImages:[self handlePage]];
//    
//    album.center = self.view.center;
//    
//    [self.view addSubview:album];
//    [album release];
//    
//}
//








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
